#pragma once
#include "..\IOInterfaces.h"
#include <list>
#include <assert.h>
#include <map>
#include <string>
#include "Unpackers\DirectLoader.h"
namespace re
{
	namespace io
	{
		class ResourceHandle : public IResourceHandle
		{
		public:
			ResourceHandle(char * buffer, U32 size) : buffer_(buffer), size_(size) { }
			~ResourceHandle() { if(buffer_ != NULL) delete [] buffer_; }
			virtual const S8* GetData() { return buffer_; }
			virtual const U32 GetSize() { return size_; }
		private:
			char * buffer_;
			U32 size_;
		};



		typedef std::map<std::wstring, ResourceUnpackerPtr> ResourceUnpackerMap;
		typedef std::list<ResourceHandlePtr> ResourceHandleList;
		typedef std::map<std::wstring, ResourceHandlePtr> ResourceHandleMap;
		class ResourceCache : public IResourceCache
		{
		public:
			ResourceCache();
			~ResourceCache();

			virtual ResourceHandlePtr GetResource(const std::wstring & filename); // is loaded? -> return, !-> load and return
			

			virtual bool AddResourceUnpacker(std::wstring & format,ResourceUnpackerPtr loader);
			virtual bool RemoveResourceUnpacker(ResourceUnpackerPtr loader);

			virtual bool ReleaseCache();

		private:
			virtual ResourceHandlePtr LoadResource(const std::wstring & filename);
			virtual ResourceHandlePtr FindResource(const std::wstring & filename);

			ResourceUnpackerMap unpackers_;
			ResourceHandleMap handles_;
		};
	}
}